﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.IO;

namespace PVP
{
    class FilterFile
    {
        private List<string> GSIfn = new List<string>();
        private List<string> NA2fn = new List<string>();

        public FilterFile(string[] files, string ApplicationPath)
        {
            if (Directory.Exists(PVP.dataPath))
            {
                Directory.Delete(PVP.dataPath, true);
            }
            Directory.CreateDirectory(PVP.dataPath);

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].EndsWith(".GSI", true, null))
                {
                    GSIfn.Add(files[i]);
                }
                if (files[i].EndsWith(".NA2", true, null))
                {
                    NA2fn.Add(files[i]);
                }
            }
            ProcessGSI(GSIfn);
        }

        private void ProcessGSI(List<string> fileNames)
        {
            string Text = string.Empty;
            string sline = string.Empty;
            string[] lineCells = null;
            foreach (string fn in fileNames)
            {
                Text = string.Empty;
                using (StreamReader sr = new StreamReader(fn))
                {
                    int count = 0;
                    while ((sline = sr.ReadLine()) != null)
                    {
                        sline = sline.Trim(new char[] { '\r', '\n', ',', '*' });
                        if (sline.Contains(" 84..") && sline.Contains(" 85.."))
                        {
                            if (Text != string.Empty)
                            {
                                string fname = fn.Substring(fn.LastIndexOf('\\'));
                                fname = fname.Substring(0, fname.LastIndexOf('.'));
                                using (StreamWriter sw = new StreamWriter(PVP.dataPath + "\\" + fname + "_" + count + ".GSI"))
                                {
                                    sw.Write(Text);
                                }
                                count++;
                                Text = string.Empty;
                            }
                            Text += "Station,";
                            Text += double.Parse(sline.Substring(31, 16)) / 10000 + ",";
                            Text += double.Parse(sline.Substring(55, 16)) / 10000 + "\n";
                            continue;
                        }
                        lineCells = sline.Split(' ');
                        for (int j = 0; j < lineCells.Length; j++)
                        {
                            if (lineCells[j].StartsWith("11"))
                            {
                                Text += lineCells[j].Substring(17) + ",";
                                continue;
                            }
                            if (lineCells[j].StartsWith("81.."))
                            {
                                Text += (double.Parse(lineCells[j].Substring(7)) / 10000) + ",";
                                continue;
                            }
                            if (lineCells[j].StartsWith("82.."))
                            {
                                Text += (double.Parse(lineCells[j].Substring(7)) / 10000) + "\n";
                                continue;
                            }
                        }
                    }
                }
                string name = fn.Substring(fn.LastIndexOf('\\'));
                using (StreamWriter sw = new StreamWriter(PVP.dataPath + "\\" + name + ".GSI"))
                {
                    sw.Write(Text);
                }

            }
        }

        private void ProcessNa2(List<string> fileNames)
        {
            string Text = string.Empty;
            string sline = string.Empty;
            string[] lineCells = null;
            foreach (string fn in fileNames)
            {
                Text = string.Empty;
                using (StreamReader sr = new StreamReader(fn))
                {
                    while ((sline = sr.ReadLine()) != null)
                    {
                        sline = sline.Trim(new char[] { '\r', '\n', ',', '*' });
                        if (sline.Contains(" 84..") && sline.Contains(" 85.."))
                        {
                            Text += "Station,";
                            Text += double.Parse(sline.Substring(31, 16)) / 10000 + ",";
                            Text += double.Parse(sline.Substring(55, 16)) / 10000 + "\n";
                            continue;
                        }
                        lineCells = sline.Split(' ');
                        for (int j = 0; j < lineCells.Length; j++)
                        {
                            if (lineCells[j].StartsWith("11"))
                            {
                                Text += lineCells[j].Substring(7) + ",";
                                continue;
                            }
                            if (lineCells[j].StartsWith("81.."))
                            {
                                Text += double.Parse(lineCells[j].Substring(7)) / 10000 + ",";
                                continue;
                            }
                            if (lineCells[j].StartsWith("82.."))
                            {
                                Text += double.Parse(lineCells[j].Substring(7)) / 10000 + "\n";
                                continue;
                            }
                        }
                    }
                }
                string fname = fn.Substring(fn.LastIndexOf('\\'));
                using (StreamWriter sw = new StreamWriter(PVP.dataPath + "\\" + fname))
                {
                    sw.Write(Text);
                }
            }
        }
    }
}


